package com.lwm.service.impl;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwm.domain.MaterialRecord;
import com.lwm.domain.ResponseResult;
import com.lwm.mapper.MaterialRecordMapper;
import com.lwm.service.MaterialRecordService;
import com.lwm.utils.Code;
import com.lwm.utils.HintMessage;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 物料档案业务层
 */
@Service
public class MaterialRecordServiceImpl implements MaterialRecordService {
    @Resource
    private MaterialRecordMapper materialRecordMapper;

    /**
     * 保存
     *
     * @param materialRecord 物料档案
     * @return {@link ResponseResult}<{@link Object}>
     */
    @Override
    public ResponseResult<Object> save(MaterialRecord materialRecord) {
        // 必填参数 物料名称、分类
        if (!StringUtils.hasText(materialRecord.getMaterialName()) ||
                materialRecord.getCategoryId() == null
        ) {
            return new ResponseResult<>(Code.SAVE_ERR, HintMessage.MUST_PARAM_HINT);
        }
        // 设置雪花id
        materialRecord.setId(IdUtil.getSnowflakeNextId());
        materialRecordMapper.insert(materialRecord);
        return new ResponseResult<>(Code.SAVE_OK, HintMessage.SAVE_OK_HINT);
    }

    /**
     * 删除
     *
     * @param ids 物料档案id
     * @return {@link ResponseResult}<{@link Object}>
     */
    @Override
    public ResponseResult<Object> remove(List<Long> ids) {
        // 没有id，给予提示
        if (ids.isEmpty()) return new ResponseResult<>(Code.DELETE_ERR, HintMessage.REMOVE_LACK_ID + "物料档案");
        // 根据id获取对应的数据，取得其中的encoding，进行拼接操作
        handleData(ids);

        int i = materialRecordMapper.deleteByIds(ids);
        if (i <= 0) {
            return new ResponseResult<>(Code.DELETE_ERR, HintMessage.REMOVE_ERR_HINT);
        }
        return new ResponseResult<>(Code.DELETE_OK, HintMessage.REMOVE_OK_HINT);
    }

    /**
     * 数据处理
     * 对于分类编码进行处理
     * 如：TM1223 拼接上 - 和 雪花id => TM123-xx...
     *
     * @param ids id集合
     */
    @Override
    public void handleData(List<Long> ids) {
        for (Long id : ids) {
            MaterialRecord materialRecord = materialRecordMapper.selectById(id);
            // 避免空指针异常
            if (!Objects.isNull(materialRecord)) {
                materialRecord.setMaterialEncoding(materialRecord.getMaterialEncoding() + "-"
                        + IdUtil.getSnowflakeNextId());
                // 执行修改
                materialRecordMapper.update(materialRecord);
            }
        }
    }

    /**
     * 编辑
     *
     * @param materialRecord 物料档案
     * @return {@link ResponseResult}<{@link Object}>
     */
    @Override
    public ResponseResult<Object> edit(MaterialRecord materialRecord) {
        // id 是否有传递过来
        if (null == materialRecord.getId()) {
            return new ResponseResult<>(Code.UPDATE_ERR, HintMessage.EDIT_LACK_ID + "物料档案");
        }
        int i = materialRecordMapper.update(materialRecord);
        if (i <= 0) {
            return new ResponseResult<>(Code.UPDATE_ERR, HintMessage.EDIT_ERR_HINT);
        }
        return new ResponseResult<>(Code.UPDATE_OK, HintMessage.EDIT_OK_HINT);
    }

    /**
     * 分页
     *
     * @param page           页码
     * @param size           数量
     * @param materialRecord 物料档案
     * @return {@link ResponseResult}<{@link PageInfo}<{@link MaterialRecord}>>
     */
    @Override
    public ResponseResult<PageInfo<MaterialRecord>> page(Integer page, Integer size, MaterialRecord materialRecord) {
        // 未传值，设置默认值
        if (null == page) page = 1;
        if (null == size) size = 5;
        PageInfo<MaterialRecord> pageInfo = PageHelper.startPage(page, size)
                .doSelectPageInfo(() -> materialRecordMapper.selectAll(materialRecord));
        return new ResponseResult<>(Code.GET_OK, HintMessage.GET_OK_HINT, pageInfo);
    }

    /**
     * 根据id获取数据
     *
     * @param id 物料档案id
     * @return {@link ResponseResult}<{@link MaterialRecord}>
     */
    @Override
    public ResponseResult<MaterialRecord> getById(Long id) {
        if (null == id) return new ResponseResult<>(Code.GET_ERR, HintMessage.GET_LACK_ID + "物料档案");
        MaterialRecord materialRecord = materialRecordMapper.selectById(id);
        if (Objects.isNull(materialRecord)) {// 数据为空
            return new ResponseResult<>(Code.GET_ERR, HintMessage.GET_ERR_HINT);
        }
        return new ResponseResult<>(Code.GET_OK, HintMessage.GET_OK_HINT, materialRecord);
    }
}
